package ar.edu.unq.wannaplay.panels.employee;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.wannaplay.dtos.TypeOfLicenceDTO;
import ar.edu.unq.wannaplay.exceptions.EmployeeCantTakeLicenceException;
import ar.edu.unq.wannaplay.exceptions.NullArgumentException;
import ar.edu.unq.wannaplay.pages.employee.EditEmployeePage;
import ar.edu.unq.wannaplay.panels.AddLicenceTypePanel;
import ar.edu.unq.wannaplay.panels.DateFromAndToPanel;
import ar.edu.unq.wannaplay.panels.FeedBackPanel;
import ar.edu.unq.wannaplay.panels.ServicedPanel;
import ar.edu.unq.wannaplay.services.EmployeeService;
import ar.edu.unq.wannaplay.utils.DateCollector;
import ar.edu.unq.wannaplay.utils.EmployeeEditer;

public class EditEmployeeLicencesPanel extends ServicedPanel<EmployeeService> {

    private static final long serialVersionUID = 1L;

    private final EmployeeEditer employeeEditer;

    private final DateCollector dateColector;

    private ModalWindow newTypeModal;

    private TypeOfLicenceDTO chosenType;

    private final FeedBackPanel feedBackPanel;

    public EditEmployeeLicencesPanel(final String id, final EmployeeEditer modelObjectRep,
            final EmployeeService service, final FeedBackPanel feedBackPanel) {
        super(id, service);
        employeeEditer = modelObjectRep;
        this.feedBackPanel = feedBackPanel;
        dateColector = new DateCollector();
        this.initialize();

    }

    private void initialize() {
        Form form = new Form("form");
        PropertyModel chosenTypeBind = new PropertyModel(this, "chosenType");

        form.add(new DropDownChoice("licencesType", chosenTypeBind, this.getService().getLicenceService()
                .getAllTypesOfLicencesDTO()));
        form.add(new DateFromAndToPanel("datePanel", this.getDateColector()));

        this.setNewTypeModal(new ModalWindow("newType"));
        this.add(this.getNewTypeModal());
        this.getNewTypeModal().setContent(
                new AddLicenceTypePanel(this.getNewTypeModal().getContentId(), this.getEmployeeEditer(), this
                        .getService()));
        form.add(this.acceptButton("accept"));

        this.add(this.ajaxToShowModal("showModal"));
        this.add(form);
    }

    private AjaxButton acceptButton(final String id) {
        return new AjaxButton(id) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                try {
                    EditEmployeeLicencesPanel.this.acceptAction();
                    this.setResponsePage(new EditEmployeePage(EditEmployeeLicencesPanel.this.getEmployeeEditer(),
                            EditEmployeeLicencesPanel.this.getService()));
                } catch (EmployeeCantTakeLicenceException e) {
                    EditEmployeeLicencesPanel.this.getFeedBackPanel().changeMessage(
                            this.getLocalizer().getString("licence.employee.error", this));
                    target.addComponent(EditEmployeeLicencesPanel.this.getFeedBackPanel());

                } catch (NullArgumentException e) {
                    EditEmployeeLicencesPanel.this.getFeedBackPanel().changeMessage(
                            this.getLocalizer().getString("error.nullcombobox", this));
                    target.addComponent(EditEmployeeLicencesPanel.this.getFeedBackPanel());
                }
            }
        };
    }

    public void acceptAction() throws EmployeeCantTakeLicenceException, NullArgumentException {
        this.getService().giveLicenceTo(this.getEmployeeEditer().getDni(), this.getChosenType(),
                this.getDateColector().toInterval());
    }

    private AjaxLink ajaxToShowModal(final String string) {
        return new AjaxLink(string) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(final AjaxRequestTarget target) {
                EditEmployeeLicencesPanel.this.getNewTypeModal().show(target);
            }
        };
    }

    public EmployeeEditer getEmployeeEditer() {
        return employeeEditer;
    }

    public DateCollector getDateColector() {
        return dateColector;
    }

    public void setChosenType(final TypeOfLicenceDTO chosenType) {
        this.chosenType = chosenType;
    }

    public TypeOfLicenceDTO getChosenType() {
        return chosenType;
    }

    public void setNewTypeModal(final ModalWindow newTypeModal) {
        this.newTypeModal = newTypeModal;
    }

    public ModalWindow getNewTypeModal() {
        return newTypeModal;
    }

    public FeedBackPanel getFeedBackPanel() {
        return feedBackPanel;
    }

}
